package com.huixh.train.member.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huixh.train.common.context.LoginMemberContext;
import com.huixh.train.common.resp.PageResult;
import com.huixh.train.member.domain.Passenger;
import com.huixh.train.member.domain.PassengerExample;
import com.huixh.train.member.mapper.PassengerMapper;
import com.huixh.train.member.req.PassengerQueryReq;
import com.huixh.train.member.req.PassengerSaveReq;
import com.huixh.train.member.resp.PassengerQueryResp;
import com.huixh.train.member.service.PassengerService;
import com.huixh.train.utils.random.SnowUtil;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author huixh
 */
@Service
public class PassengerServiceImpl implements PassengerService {

    private final Logger logger = LoggerFactory.getLogger(PassengerServiceImpl.class);
    @Resource
    private PassengerMapper passengerMapper;
    @Resource
    private SnowUtil snowUtil;

    @Override
    public void save(PassengerSaveReq passengerSaveReq) {
        DateTime now = DateTime.now();
        Passenger passenger = BeanUtil.copyProperties(passengerSaveReq, Passenger.class);
        passenger.setMemberId(LoginMemberContext.getMemberId());
        if (ObjectUtil.isNotNull(passengerSaveReq.getId())) {
            passenger.setUpdateTime(now);
            passengerMapper.updateByPrimaryKey(passenger);
        } else {
            passenger.setId(snowUtil.getSnowflakeToLong());
            passenger.setCreateTime(now);
            passenger.setUpdateTime(now);
            passengerMapper.insert(passenger);
        }
    }

    @Override
    public PageResult<List<PassengerQueryResp>> queryList(PassengerQueryReq passengerQueryReq) {
        PassengerExample passengerExample = new PassengerExample();
        PassengerExample.Criteria criteria = passengerExample.createCriteria();
        passengerExample.setOrderByClause("create_time desc");

        if (ObjectUtil.isNotNull(passengerQueryReq.getMemberId())) {
            criteria.andMemberIdEqualTo(passengerQueryReq.getMemberId());
        }
        logger.info("查询页码:{}, 查询条数:{}", passengerQueryReq.getPageNum(), passengerQueryReq.getPageSize());
        PageHelper.startPage(passengerQueryReq.getPageNum(), passengerQueryReq.getPageSize());
        List<Passenger> passengerList = passengerMapper.selectByExample(passengerExample);
        PageInfo<Passenger> pageInfo = new PageInfo<>(passengerList);
        logger.info("总行数:{}, 总页数:{}", pageInfo.getTotal(), pageInfo.getPages());
        List<PassengerQueryResp> queryResp = BeanUtil.copyToList(passengerList, PassengerQueryResp.class);
        return PageResult.success(pageInfo.getTotal(), queryResp);
    }

    @Override
    public void delete(Long id) {
        passengerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<PassengerQueryResp> queryMine() {
        Long memberId = LoginMemberContext.getMemberId();
        PassengerExample passengerExample = new PassengerExample();
        PassengerExample.Criteria criteria = passengerExample.createCriteria();
        criteria.andMemberIdEqualTo(memberId);
        List<Passenger> passengerList = passengerMapper.selectByExample(passengerExample);
        return BeanUtil.copyToList(passengerList, PassengerQueryResp.class);
    }
}
